package mfuny.ggongal.admin.ad;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import mfuny.ggongal.admin.Server;
import mfuny.ggongal.admin.games.GameDao;
import mfuny.ggongal.admin.gift.GiftCardDao;
import mfuny.ggongal.admin.gift.GiftDao;
import mfuny.ggongal.admin.timetable.TimeDao;

import org.springframework.web.servlet.ModelAndView;

import com.ibatis.sqlmap.client.SqlMapClient;

public class AdTimeService {
	SqlMapClient sqlMapper;
	
	ModelAndView mav;
	
	AdDao resultClass;
	GameDao gameClass;
	TimeDao timeClass;
	TimeDao timedClass;
	List<AdDao> resultList;
	List<GameDao> gameList;
	List<GiftDao> giftList;
	List<HashMap<String, Object>> mapList;
	HashMap<String, TimeDao> map;
	
	public AdTimeService() throws Exception {
		sqlMapper = Server.getSqlMapper( );
		
		mav = new ModelAndView();
		resultClass = new AdDao();
		gameClass = new GameDao();
		timeClass = new TimeDao();
		timedClass = new TimeDao();
		resultList = new ArrayList<AdDao>();
		giftList = new ArrayList<GiftDao>();
		mapList = new ArrayList<HashMap<String, Object>>();
		map = new HashMap<String, TimeDao>();
	}
	
	//Ÿ���̺�Ʈ ����(��Ȳ).
	@SuppressWarnings( "unchecked" )
	public ModelAndView timeEvent() throws Exception {
//		resultList = sqlMapper.queryForList( "ad_selectTimeAll", null );
		mapList.clear( );
		mapList = sqlMapper.queryForList( "ad_selectForMapByService", "time" );
		mav.clear( );
		mav.setViewName( "/ad/time/timeEvent.ga" );
		mav.addObject( "mapList", mapList );
		mav.addObject( "listSize", mapList.size() );
		return mav;
	}
	
	//Ÿ���̺�Ʈ ���0.
	public ModelAndView regTimeEvent0() throws Exception {
		mav.clear( );
		mav.setViewName( "/ad/time/regTimeEvent0.ga" );
		return mav;
	}
	
	//Ÿ���̺�Ʈ ���1(Ÿ��).
	public ModelAndView regTimeEvent1() throws Exception {
		map.clear( );
		mav.clear( );
		mav.setViewName( "/ad/time/regTimeEvent1.ga" );
		return mav;
	}
	
	//Ÿ���̺�Ʈ ���2(����).
	@SuppressWarnings( "unchecked" )
	public ModelAndView regTimeEvent2() throws Exception {
		gameList = sqlMapper.queryForList( "games_selectAll", null );
		mav.clear( );
		mav.setViewName( "/ad/time/regTimeEvent2.ga" );
		mav.addObject( "gameList", gameList );
		return mav;
	}
	
	//Ÿ���̺�Ʈ ���3(�̼�).
	@SuppressWarnings( "unchecked" )
	public ModelAndView regTimeEvent3( AdDao paramClass ) throws Exception {
		gameClass = (GameDao) sqlMapper.queryForObject( "games_selectNo", paramClass.getGame_no( ) );
		giftList = sqlMapper.queryForList( "gift_selectForContents", paramClass.getContents_no( ) );
		List<GiftCardDao> giftCardList = sqlMapper.queryForList("card_selectAll", null);
		
		mav.clear( );
		mav.setViewName( "/ad/time/regTimeEvent3.ga" );
		mav.addObject( "gameClass", gameClass );
		mav.addObject( "giftList", giftList );
		mav.addObject("giftCardList", giftCardList);
		return mav;
	}
	
	//Ÿ���̺�Ʈ ���4(��¥).
	public ModelAndView regTimeEvent4() throws Exception {
		map.clear( );
		mav.clear( );
		mav.setViewName( "/ad/time/regTimeEvent4.ga" );
		return mav;
	}
	
	//�ð�����.
	public ModelAndView timeSelection( String days ) throws Exception {
		mav.clear( );
		int isDays = (Integer) sqlMapper.queryForObject( "time_isDays", days );
		if ( isDays > 0 ) {
			timedClass = (TimeDao) sqlMapper.queryForObject( "time_selectOne", days );
			mav.addObject( "timedClass", timedClass );
		}
		if ( map.containsKey( days ) ) {
			timeClass = map.get( days );
			mav.addObject( "timeClass", timeClass );
		}
		mav.setViewName( "/ad/time/timeSelection" );
		return mav;
	}
	
	//�ð����� �ӽ������.
	public ModelAndView selectedTimes( String days, String h15, String h18, String h21 ) throws Exception {
		TimeDao timeClass = new TimeDao();
		timeClass.setDays( days );
		timeClass.setH15( h15 );
		timeClass.setH18( h18 );
		timeClass.setH21( h21 );
		map.put( days, timeClass );
		mav.clear( );
		mav.setViewName( "/ad/time/blank" );
		return mav;
	}
	
	//Ÿ���̺�Ʈ ����Ȯ��.
	public ModelAndView regTimeEventCheck( HttpSession session ) throws Exception {
		resultClass = (AdDao) session.getAttribute( "timeEvent" );
		resultClass.setService( "time" );
		resultClass.setDays( setDaysFinal() );
		session.setAttribute( "timeEvent", resultClass );
		mav.clear( );
		mav.setViewName( "/ad/time/regTimeEventCheck.ga" );
		mav.addObject( "resultClass", resultClass );
		return mav;
	}
	
	//������� db���.
	public void regTimeEventFinal( HttpSession session ) throws Exception {
		resultClass = (AdDao) session.getAttribute( "timeEvent" );
		resultClass.setRegdate( new Timestamp( System.currentTimeMillis( ) ));
		try {
			sqlMapper.startTransaction( );
			sqlMapper.update( "ad_insert", resultClass );
			int ad_no = (Integer) sqlMapper.queryForObject( "ad_selectLastNo", null );
			Collection<TimeDao> values = map.values( );
			for ( TimeDao td : values ) {
				if ( td.getH15( ).equals( "false" ) && td.getH18( ).equals( "false" ) && td.getH21( ).equals( "false" ) ) {
					continue;
				}
				if ( td.getH15( ).equals( "true" ) ) {
					td.setH15( String.valueOf( ad_no ) );
				} else {
					td.setH15( null );
				}
				if ( td.getH18( ).equals( "true" ) ) {
					td.setH18( String.valueOf( ad_no ) );
				} else {
					td.setH18( null );
				}
				if ( td.getH21( ).equals( "true" ) ) {
					td.setH21( String.valueOf( ad_no ) );
				} else {
					td.setH21( null );
				}
				int isDays = (Integer) sqlMapper.queryForObject( "time_isDays", td.getDays( ) );
				
				if ( isDays > 0 ) {
					sqlMapper.update( "time_updateTimeGame", td );
				} else {
					sqlMapper.update( "time_insertTimeGame", td );
				}
			}
			sqlMapper.commitTransaction( );
		} finally {
			sqlMapper.endTransaction( );
		}
	}
	
	//��¥����.
	private String setDaysFinal( ) {
		String finalDays = "";
		Collection<TimeDao> values = map.values( );
		for ( TimeDao td : values ) {
			if ( td.getH15( ).equals( "false" ) && td.getH18( ).equals( "false" ) && td.getH21( ).equals( "false" ) ) {
				continue;
			}
			finalDays += (finalDays.equals( "" ) ? "" : ",") + td.getDays( );
			if ( td.getH15( ).equals( "true" ) ) {
				finalDays += "/15";
			} else if ( td.getH18( ).equals( "true" ) ) {
				finalDays += "/18";
			} else if ( td.getH21( ).equals( "true" ) ) {
				finalDays += "/21";
			}
		}
		return finalDays;
	}
}
